package com.zt.bigdata.vertx.controller

import com.typesafe.scalalogging.LazyLogging
import com.zt.bigdata.vertx.interceptor.Animal
import com.zt.bigdata.vertx.service._
import io.vertx.core.Vertx
import io.vertx.core.http.HttpMethod
import io.vertx.ext.web.Router
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration

/**
  *
  */
@Configuration
class ControllerFactory @Autowired()
(optionsHandler: OptionsHandler, methodNotFoundHandler: MethodNotFoundHandler, aHandler: AHandler, basicHandler: BasicHandler[Animal, String])
  extends LazyLogging {
  val A_PATH = "/.*"

  def initRouter(vertx: Vertx): Router = {
    val router = Router.router(vertx)

    router.post.pathRegex(A_PATH)
      .handler(basicHandler.toHandler(aHandler))

    val allPattern = Array(A_PATH).toStream.mkString("(?:", ")|(?:", ")")

    //    SDK跨域使用
    router
      .options
      .pathRegex(allPattern)
      .handler(optionsHandler)

    router
      .route
      .method(HttpMethod.GET)
      .method(HttpMethod.POST)
      .method(HttpMethod.PUT)
      .method(HttpMethod.DELETE)
      .method(HttpMethod.TRACE)
      .method(HttpMethod.CONNECT)
      .method(HttpMethod.PATCH)
      .method(HttpMethod.OTHER)
      .pathRegex(A_PATH)
      .handler(methodNotFoundHandler)
    router
  }


}

